package cn.chenxiejia;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;

/***
 * @program: maven
 * @ClassName: Ms1
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/1/14 22:56
 * @version : V1.0
 */
public class Ms1 {
    public static void main(String[] args) {
        //求123456789 字符串1..9 之间不改数字位置随机+-要求表达结果和为100,写出这样表达式
        String[] str = new String[]{"", "-", "+"};
        StringBuilder num = new StringBuilder();
        List<String> list = new ArrayList<>();
        Random rand = new Random();
        int w = 0;
        while (w < 11) {
            for (int i = 1; i < 9; i++) {
                num.append(i + str[rand.nextInt(3)]);
            }
            num.append("9");


            if (is(num.toString())) {
                System.out.println(num + "=100");
                w++;
            }
            num.delete(0, num.length());
        }

    }

    public static boolean is(String s) {
        boolean f = false;
        Pattern pattern = Pattern.compile("[-+]?\\d+");
        var a = pattern.matcher(s);
        int num = 0;
        while (a.find()) {
            num += Integer.parseInt(a.group());
            if (num == 100) {
                f = true;
            }
        }
        return f;
    }
}
